package cn.yzx.pethome.system.pet.service.impl;

import cn.yzx.pethome.constants.BaseConstants;
import cn.yzx.pethome.jwt.LoginContextUtils;
import cn.yzx.pethome.system.pet.domain.Pet;
import cn.yzx.pethome.system.pet.domain.PetDetail;
import cn.yzx.pethome.system.pet.domain.PetOnlineAuditLog;
import cn.yzx.pethome.system.pet.mapper.PetDetailMapper;
import cn.yzx.pethome.system.pet.mapper.PetMapper;
import cn.yzx.pethome.system.pet.mapper.PetOnlineAuditLogMapper;
import cn.yzx.pethome.system.pet.query.PetQuery;
import cn.yzx.pethome.system.pet.service.IPetService;
import cn.yzx.pethome.base.service.impl.BaseServiceImpl;
import cn.yzx.pethome.system.user.domain.Employee;
import cn.yzx.pethome.system.user.mapper.EmployeeMapper;
import cn.yzx.pethome.utils.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanzixuan
 * @since 2023-02-05
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private LoginContextUtils loginContextUtils;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthService authService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    /**
     * 上下架  状态
     * 创建时间
     * 狗的归属的店铺  谁去录入就获取哪个店铺的id
     * 详细信息 没有关联上
     * @param pet
     */
    public void add(Pet pet, HttpServletRequest request) {
        Employee employee = (Employee) loginContextUtils.getLoginUser(request);
        pet.setState(BaseConstants.Pet.STATE_DOWM);
        pet.setCreatetime(new Date());
        Employee one = employeeMapper.findOne(employee.getId());

        pet.setShopId(one.getShopId());
        super.add(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);
    }

    @Override
    public List<Pet> findByQuery(PetQuery query, HttpServletRequest request) {

        getCurrentShopId(query, request);
        return super.findByQuery(query);
    }

    /**
     * 获取当前用户的店铺id
     * @param query
     * @param request
     * @return
     */
    private void getCurrentShopId(PetQuery query, HttpServletRequest request) {
        //获取当前用户的店铺id
        Employee employee = (Employee) loginContextUtils.getLoginUser(request);

        Employee one = employeeMapper.findOne(employee.getId());
        Long shopId = one.getShopId();
        if (shopId != null) {
            query.setShopId(shopId);
        }
    }


    @Override
    public Long findByQueryCount(PetQuery query, HttpServletRequest request) {
        getCurrentShopId(query, request);
        return super.findByQueryCount(query);
    }

    @Override
    public String onsale(List<Long> ids) {
        List<Pet> petList = petMapper.findByIds(ids);
        //得到全是下架的
        List<Pet> offPetList = petList.stream().filter(pet -> pet.getState() == 0).collect(Collectors.toList());
        //现在有两种情况  有审核通过(直接上架)  有审核失败的(提示给用户)
        List<Pet> errorPet = new ArrayList<>();

        offPetList.forEach(pet->{
            if(authService.authentication(pet.getName())){
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setState(BaseConstants.Pet.STATE_UP);
                petOnlineAuditLog.setNote("审核通过");
                petOnlineAuditLogMapper.add(petOnlineAuditLog);

                pet.setState(BaseConstants.Pet.STATE_UP);
                pet.setOnsaletime(new Date());
                petMapper.update(pet);
            }else{
                errorPet.add(pet);
            }
        });
        if(errorPet.isEmpty()){
            return "上架成功";
        }
        String errorMsg = "存在非法内容:";
        for (int i = 0; i < errorPet.size(); i++) {
            if(i == 0){
                errorMsg = errorMsg + errorPet.get(i).getName();
            }else {
                errorMsg = errorMsg + "、" + errorPet.get(i).getName();
            }
        }
        return errorMsg;
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }


    @Override
    public void delete(Long id) {
        //如果要删除图片
        //先用id把数据查询回来
        //在去调用工具类去删除（循环删除，要用分割路径）
        //子表里面也有图片的路径
        super.delete(id);
        petDetailMapper.deleteDetailByPetId(id);
    }


}
